/**
 * 
 */
package com.point.business.parsers;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.point.business.database.model.InformationUOPB;
import com.point.business.util.ParserException;

/**
 * @author Ilija Trajkoski
 * 
 */
public class ParserUOPB implements ExcelParser<InformationUOPB> {
	private static Logger logger = Logger.getLogger(ParserUOPB.class);

	public List<InformationUOPB> parse(InputStream file) throws IOException,
			ParserException {
		List<InformationUOPB> result = new ArrayList<InformationUOPB>();

		XSSFWorkbook workbook = new XSSFWorkbook(file);
		XSSFSheet sheet = workbook.getSheetAt(0);
		Iterator<Row> rowIterator = sheet.iterator();
		int rowCount = 2;
		if (rowIterator.hasNext()) {
			// Skipping first row because it contains captions
			rowIterator.next();
		}
		while (rowIterator.hasNext()) {
			Row row = rowIterator.next();
			InformationUOPB informationUOPB = new InformationUOPB();
			int cellCount = 0;
			try {
				Cell cell = row.getCell(cellCount, Row.CREATE_NULL_AS_BLANK);
				informationUOPB.setProcedure(cell.getStringCellValue());
				cellCount++;
				cell = row.getCell(cellCount, Row.CREATE_NULL_AS_BLANK);
				informationUOPB.setTitle(cell.getStringCellValue());
				cellCount++;
				cell = row.getCell(cellCount, Row.CREATE_NULL_AS_BLANK);
				informationUOPB.setEmbs(String.valueOf(new Double(cell
						.getNumericCellValue()).longValue()));
				cellCount++;
				cell = row.getCell(cellCount, Row.CREATE_NULL_AS_BLANK);
				Date anounceDate = cell.getDateCellValue();
				informationUOPB.setAnounceDate(String.valueOf(anounceDate));
				informationUOPB.setAnounceDatum(anounceDate);
				cellCount++;
				cell = row.getCell(cellCount, Row.CREATE_NULL_AS_BLANK);
				informationUOPB.setUuid(String.valueOf(new Double(cell
						.getNumericCellValue()).longValue()));
				cellCount++;
				cell = row.getCell(cellCount, Row.CREATE_NULL_AS_BLANK);
				informationUOPB.setDesctription(cell.getStringCellValue());
				cellCount++;
				cell = row.getCell(cellCount, Row.CREATE_NULL_AS_BLANK);
				informationUOPB.setAnounceBy(cell.getStringCellValue());
				cellCount++;
				cell = row.getCell(cellCount, Row.CREATE_NULL_AS_BLANK);
				if (cell != null) {
					informationUOPB.setSlv(String.valueOf(new Double(cell
							.getNumericCellValue()).longValue()));
					cellCount++;
				}
			} catch (Exception e) {
				logger.error(e);
				char column = Character.toChars(64 + cellCount)[0];
				throw new ParserException("Содржината на полето: " + column
						+ "-" + +rowCount + " е невалидна!");
			}
			rowCount++;
			result.add(informationUOPB);
		}
		return result;
	}

}
